function createElement(selector) {
  if(typeof selector !== 'string') {
    console.error(`create element: selector-${selector} not string`);
    return;
  }

  let eles = selector.split(/\s*>\s*/);
  eles.forEach((val, idx, arr) => {
    arr[idx] = arr[idx].trim();
  });
  let root;
  if(eles.length === 1) {
    return document.createElement(selector);
  } else {
    eles.reduce((pre, cur) => {
      if(typeof pre === 'string') {
        pre = document.createElement(pre);
        root = pre;
        pre = [pre];
      }
      let resArr = [];
      pre.forEach((parent) => {
        let tuple = cur.split(/\s*\*\s*/);
        if(tuple[1]) tuple[1] = parseInt(tuple[1]);
        else tuple[1] = 1;
        for(let i = 0; i < tuple[1]; i++) {
          let node = document.createElement(tuple[0]);
          parent.appendChild(node);
          resArr.push(node);
        }
      })
      return resArr;
    });
  }
  return root;
}

function createElementPath(selector) {
  if(typeof selector !== 'string') {
    console.error(`create element path: selector-${selector} not string`);
    return;
  }

  let pathTag = selector.split(/\s*>\s*/);
  let path = new Array(pathTag.length);
  pathTag.forEach((tagName, idx) => {
    path[idx] = document.createElement(tagName);
  });
  path.reduce((pre, cur) => {
    pre.appendChild(cur);
  });
  return path;
}

export {
  createElement,
  createElementPath
}